var lastMeetingId = null
function openEditMeetingWindow(flag) {
    var selection = meeting.getSelection()
    var startTime 
    if (flag=="修改") {
        startTime = (new Date(selection[0].data.startTime))
    }
    var meetingEditWindow = Ext.create('Ext.window.Window', {
        title: flag+"会议",
        iconCls:'x-fa fa-book',
        width: 350,
        modal:true,
        items:[
            {
                xtype:'form',
                width:'100%',
                height:'100%',
                border:false,
                margin: '5 0 0 0',

                items:[
                    {
                        xtype:'hidden',
                        id:'id',
                        value:flag=="修改"?selection[0].data.id:null
                    },
                    {
                        xtype: 'textfield',
                        id:'meetingCnName',
                        labelAlign:'right',
                        allowBlank:false,
                        fieldLabel:'会议名称',
                        value:flag=="修改"?selection[0].data.cnName:""
                    },
                    {
                        xtype: 'datefield',
                        id:'meetingStartDate',
                        labelAlign:'right',
                        allowBlank:false,
                        editable:false,
                        format:'Y-m-d',
                        minValue:(new Date()),
                        fieldLabel:'开始日期',
                        value:flag=="修改"?startTime:""
                    }
                    ,
                    {
                        xtype: 'timefield',
                        id:'meetingStartTime',
                        labelAlign:'right',
                        allowBlank:false,
                        editable:false,
                        increment: 5,
                        format:'H:i',
                        fieldLabel:'开始时间',
                        value:flag=="修改"?startTime:""
                    }
                ],
                buttons: [
                    {
                        xtype: 'button',
                        text: '取消',
                        handler: function() {
                            meetingEditWindow.close()
                        }
                    },
                    {
                         xtype: 'button',
                         formBind:true,
                         text: '确定',
                         handler: function() {

                            Ext.Ajax.request({
                                url: "../meeting/save",
                                params: {
                                   id:Ext.getCmp("id").getValue(),
                                   cnName:Ext.getCmp("meetingCnName").getValue(),
                                   startDate:Ext.Date.format(Ext.getCmp("meetingStartDate").getValue(), "Y-m-d"),
                                   startTime:Ext.Date.format(Ext.getCmp("meetingStartTime").getValue(), "H:i")
                                },
                                success:function(response, opts) {
                                    var res = Ext.JSON.decode(response.responseText)
                                    if(!res.success) {
                                        Ext.MessageBox.show({
                                            title: '出错!',
                                            msg:res.message,
                                            buttons: Ext.MessageBox.OK,
                                            icon: Ext.MessageBox["ERROR"]
                                        });
                                    } else {
                                        Ext.toast('操作成功!');
                                        lastMeetingId = res.id
                                        meetingStore.load()
                                    }

                                    meetingEditWindow.close()
                                    
                                    
                                }
                            })

                           
                         }
                    }
                ]
            }
        ]
    })
    meetingEditWindow.show()


}



var meetingStore = Ext.create('Ext.data.Store', {
    autoLoad: true,
    pageSize: 1000,
    remoteSort: true,
    remoteFilter:true,
    //sorters: [{property:"a.id",direction:"DESC"}],
    listeners:{
        beforeload:function() {
            
        },
        load:function( obj, records, successful, operation, eOpts ) {
            console.log(lastMeetingId)
            if (lastMeetingId==null) {
                meeting.getSelectionModel().select(0)
            } else {
                for(var i=0;i<records.length;i++) {
                    if (lastMeetingId==records[i].data.id) {
                        meeting.getSelectionModel().select(records[i])
                        console.log(records[i])
                        break
                    }
                }
            }
        }
    },
    proxy: {
        type: 'ajax',
        url : '../meeting/store',
        reader: {
            type: 'json',
            root: 'results',
            totalProperty: 'total'
        }
    }
});





var meeting = Ext.create('Ext.grid.Panel', {
    title: '会议列表',
    flex:1,
    viewConfig: {
        enableTextSelection: true
        /*getRowClass: function(record, rowIndex, rowParams, store){
           return record.get("rowStyle");
        }*/
    },
    selModel: {
       selType: 'rowmodel'
    },
    listeners:{
        select:function(obj, record, index, eOpts){

            file.setTitle(record.data.cnName+""+"&nbsp;文件列表")
            Ext.getCmp('btnEditMeeting').enable()
            Ext.getCmp('btnDelMeeting').enable()
            Ext.getCmp('btnFileUpload').enable()
            if (record.data.status==0) {
                Ext.getCmp('btnCopyMeeting').disable()
                Ext.getCmp('btnStartMeeting').enable()
                Ext.getCmp('btnStopMeeting').disable()
            }
            if (record.data.status==1) {
                Ext.getCmp('btnCopyMeeting').disable()
                Ext.getCmp('btnStartMeeting').disable()
                Ext.getCmp('btnStopMeeting').enable()
            }
            if (record.data.status==2) {
                Ext.getCmp('btnCopyMeeting').enable()
                Ext.getCmp('btnStartMeeting').disable()
                Ext.getCmp('btnStopMeeting').disable()
            }

            fileStore.proxy.extraParams = {meetingId:record.data.id}
            fileStore.load()
        }
                               
    },
    store: meetingStore,
    tbar: [
        { 
            xtype: 'button', id:'btnAddMeeting',text: '增加',iconCls:'x-fa fa-plus',
            handler:function() {
                openEditMeetingWindow('增加')
            } 
        },
        { 
            xtype: 'button', id:'btnEditMeeting',text: '修改',iconCls:'x-fa fa-pencil',disabled:true,
            handler:function() {
                openEditMeetingWindow('修改')
            } 
        },
        { 
            xtype: 'button', id:'btnCopyMeeting',text: '复制',iconCls:'x-fa fa-copy',disabled:true,
            handler:function() {
                Ext.MessageBox.confirm('确认', '是否复制该会议?', function(btn, text) {
                    if (btn=="yes") {
                        Ext.getCmp("btnDelMeeting").disable()
                        Ext.getCmp("btnEditMeeting").disable()
                        Ext.getCmp("btnCopyMeeting").disable()
                        Ext.getCmp("btnStartMeeting").disable()
                        Ext.getCmp("btnStopMeeting").disable()
                        var selection = meeting.getSelection()
                        Ext.Ajax.request({
                            url: "../meeting/copy",
                            params: {
                                id:selection[0].data.id,
                            },
                            success:function(response, opts) {

                                var res = Ext.JSON.decode(response.responseText)
                                if(!res.success) {
                                    Ext.MessageBox.show({title: '出错!',msg:res.message,buttons: Ext.MessageBox.OK,
                                    icon: Ext.MessageBox["ERROR"]});
                                } else {
                                    Ext.toast('操作成功!');
                                    lastMeetingId = res.id
                                    meetingStore.load()
                                }

                            }
                       })
                    }
                }, this);    
            } 
        },
        { 
            xtype: 'button', id:'btnDelMeeting',text: '删除',iconCls:'x-fa fa-minus',disabled:true,
            handler:function() {
                Ext.MessageBox.confirm('确认', '删除会议将删除该会议下的所有文件，是否继续?', function(btn, text) {
                    if (btn=="yes") {
                        Ext.getCmp("btnDelMeeting").disable()
                        Ext.getCmp("btnEditMeeting").disable()
                        Ext.getCmp("btnCopyMeeting").disable()
                        Ext.getCmp("btnStartMeeting").disable()
                        Ext.getCmp("btnStopMeeting").disable()

                        Ext.getCmp("btnEditFileType").disable()
                        Ext.getCmp("btnFileDelete").disable()
                        Ext.getCmp("btnMoveUp").disable()
                        Ext.getCmp("btnMoveDown").disable()


                        var selection = meeting.getSelection()
                        Ext.Ajax.request({
                            url: "../meeting/delete",
                            params: {
                                   id:selection[0].data.id,
                                },
                                success:function(response, opts) {

                                    var res = Ext.JSON.decode(response.responseText)
                                    if(!res.success) {
                                        Ext.MessageBox.show({
                                            title: '出错!',
                                            msg:res.message,
                                            buttons: Ext.MessageBox.OK,
                                            icon: Ext.MessageBox["ERROR"]
                                        });
                                    } else {
                                        Ext.toast('操作成功!');
                                        lastMeetingId = null
                                        lastFileId = null
                                        meetingStore.load()
                                        fileStore.reload()
                                    }

                                    
                                }
                       })
                    }
                }, this);
            } 
        }
        ,'-',
        { 
            xtype: 'button', id:'btnStartMeeting',text: '开始',iconCls:'x-fa fa-play',disabled:true,
            handler:function() {
                Ext.MessageBox.confirm('确认', '是否开始会议?', function(btn, text) {
                    if (btn=="yes") {
                        Ext.getCmp("btnDelMeeting").disable()
                        Ext.getCmp("btnEditMeeting").disable()
                        Ext.getCmp("btnCopyMeeting").disable()
                        Ext.getCmp("btnStartMeeting").disable()
                        Ext.getCmp("btnStopMeeting").disable()
                        var selection = meeting.getSelection()
                        Ext.Ajax.request({
                            url: "../meeting/start",
                            params: {
                                id:selection[0].data.id,
                            },
                            success:function(response, opts) {

                                var res = Ext.JSON.decode(response.responseText)
                                if(!res.success) {
                                    Ext.MessageBox.show({title: '出错!',msg:res.message,buttons: Ext.MessageBox.OK,
                                    icon: Ext.MessageBox["ERROR"]});
                                } else {
                                    Ext.toast('操作成功!');
                                    lastMeetingId = res.id
                                    Ext.getCmp("btnDelMeeting").enable()
                                    Ext.getCmp("btnEditMeeting").enable()
                                    Ext.getCmp("btnCopyMeeting").disable()
                                    Ext.getCmp("btnStartMeeting").disable()
                                    Ext.getCmp("btnStopMeeting").enable()
                                    meetingStore.reload()
                                    fileStore.proxy.extraParams = {meetingId:res.id}
                                    fileStore.load()
                                }

                            }
                       })
                    }
                }, this);    
            } 
        },
        { 
            xtype: 'button', id:'btnStopMeeting', text: '结束',iconCls:'x-fa fa-stop',disabled:true,
            handler:function() {
                Ext.MessageBox.confirm('确认', '是否结束会议?', function(btn, text) {
                    if (btn=="yes") {
                        Ext.getCmp("btnDelMeeting").disable()
                        Ext.getCmp("btnEditMeeting").disable()
                        Ext.getCmp("btnCopyMeeting").disable()
                        Ext.getCmp("btnStartMeeting").disable()
                        Ext.getCmp("btnStopMeeting").disable()
                        var selection = meeting.getSelection()
                        Ext.Ajax.request({
                            url: "../meeting/stop",
                            params: {
                                id:selection[0].data.id,
                            },
                            success:function(response, opts) {

                                var res = Ext.JSON.decode(response.responseText)
                                if(!res.success) {
                                    Ext.MessageBox.show({title: '出错!',msg:res.message,buttons: Ext.MessageBox.OK,
                                    icon: Ext.MessageBox["ERROR"]});
                                } else {
                                    Ext.toast('操作成功!');
                                    lastMeetingId = res.id
                                    Ext.getCmp("btnDelMeeting").enable()
                                    Ext.getCmp("btnEditMeeting").enable()
                                    Ext.getCmp("btnCopyMeeting").enable()
                                    Ext.getCmp("btnStartMeeting").disable()
                                    Ext.getCmp("btnStopMeeting").disable()
                                    meetingStore.reload()
                                    fileStore.proxy.extraParams = {meetingId:res.id}
                                    fileStore.load()
                                }

                            }
                       })
                    }
                }, this);    
            } 
        }
        
    ],
    columns: [
        { 
            text: '会议名称', 
            dataIndex: 'cnName',
            sortable:false,
            flex:1 /*,
            renderer:function(value,metaData,record) {
                return "<span style='font-weight:bold'>"+value+"</span>"
            }*/
        },
        { 
            text: '开始时间', 
            dataIndex: 'startTime', 
            width: 90,
            sortable:false,
            xtype: 'datecolumn',
            format: 'm-d H:i'
        },
        { 
            text: '创建时间', 
            dataIndex: 'dateCreated', 
            width: 90,
            sortable:false,
            xtype: 'datecolumn',
            format: 'm-d H:i' 
        },
        { 
            text: '状态', 
            dataIndex: 'status', 
            sortable:false,
            width: 60 ,
            renderer:function(value,metaData,record) {
                if (value==0) {
                    return "<span style='color:blue;font-weight:bold'>未开始</span>"
                }
                if (value==1) {
                    return "<span style='color:red;font-weight:bold'>进行中</span>"
                }
                if (value==2) {
                    return "<span style='color:gray;font-weight:bold'>已结束</span>"
                }
            }
        }
    ],
    //height: '100%',
    width:'100%'
});